package com.extweb4j.core.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.extweb4j.core.enums.RowStatus;
import com.extweb4j.core.kit.ExtKit;
import com.extweb4j.core.kit.LoginKit;
import com.jfinal.plugin.activerecord.Page;


/**
 * 系统消息
 * @author Administrator
 *
 */
public class ExtMsg extends CoreModel<ExtMsg>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static ExtMsg dao = new ExtMsg();

	public Page<ExtMsg> paginateByKeywaord(int page, int limit,String keywords,String uid) {
		String select  =  "SELECT m.id,m.msg_title,m.level,m.msg_content,m.uid,u.action_time,u.status";
		StringBuffer form = new StringBuffer(" FROM ext_msg m  ");
		
		form.append(" LEFT JOIN  ext_msg_user u ON u.msg_id = m.id");
		List<Object> paras = new ArrayList<Object>();
		
		form.append(" WHERE u.status != ? ");
		paras.add(RowStatus.删除.getState());
		//判断是否超级管理员
		if(!LoginKit.isAdmin(uid)){
			
			form.append(" AND u.user_id = ? ");
			paras.add(uid);
		}
		//模糊查询
		if(StringUtils.isNotBlank(keywords)){
			keywords = ExtKit.sqlFilterValidate(keywords);
			form.append(" AND (m.msg_title LIKE '%"+keywords+"%' OR m.msg_content LIKE '%"+keywords+"%' ");
		}
		return super.paginate(page, limit, select, form.toString(), paras.toArray());
	}
	
	public ExtMsg findFirstById(String msg_id){
		String sql = "SELECT mgs_title,msg_content FROM ext_msg WHERE id=?";
		return ExtMsg.dao.findFirst(sql,msg_id);
	}
}
